package com.uvnos4j.matilda.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uvnos4j.matilda.commons.annotation.AppLog;
import com.uvnos4j.matilda.commons.dto.GridTable;
import com.uvnos4j.matilda.commons.dto.PageParam;
import com.uvnos4j.matilda.commons.dto.QueryParam;
import com.uvnos4j.matilda.commons.dto.Result;
import com.uvnos4j.matilda.commons.utils.RequestUtil;
import com.uvnos4j.matilda.commons.utils.TreeUtil;
import com.uvnos4j.matilda.system.domain.SysAcl;
import com.uvnos4j.matilda.system.domain.SysRole;
import com.uvnos4j.matilda.system.domain.SysUser;
import com.uvnos4j.matilda.system.service.SysAclService;
import com.uvnos4j.matilda.system.service.SysRoleService;
import com.uvnos4j.matilda.system.service.SysUserService;
import com.uvnos4j.matilda.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色 Controller
 * <p>
 * Description:角色 Controller
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @see com.uvnos4j.matilda.system.controller
 * @since 2020-08-31 23:24:34
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysAclService sysAclService;

    @AppLog(title = "角色管理")
    @PreAuthorize("hasAuthority('sys:role')")
    @GetMapping("/list")
    public String index() {
        return "sys/rolelist";
    }

    @AppLog(title = "添加角色")
    @PreAuthorize("hasAuthority('sys:role:add')")
    @ResponseBody
    @PostMapping
    public Result add(@Valid RoleVo roleVo) {
        SysRole sysRole = roleVo.buildModel(SysRole.class, false);
        boolean result = sysRoleService.save(sysRole);
        if (result) {
            return Result.success("角色添加成功", sysRole.getId().toString());
        }
        return Result.fail("角色添加失败");
    }

    @AppLog(title = "修改角色")
    @PreAuthorize("hasAuthority('sys:role:update')")
    @ResponseBody
    @PutMapping
    public Result update(@Valid RoleVo dictVo) {
        SysRole sysRole = dictVo.buildModel(SysRole.class, true);
        boolean result = sysRoleService.updateById(sysRole);
        if (result) {
            return Result.success("角色修改成功", sysRole.getId().toString());
        }
        return Result.fail("角色修改失败");
    }

    @AppLog(title = "删除角色")
    @PreAuthorize("hasAuthority('sys:role:del')")
    @ResponseBody
    @DeleteMapping
    public Result delete(@RequestParam("ids") List<Long> idList) {
        boolean result = sysRoleService.removeByIds(idList);
        if (result) {
            return Result.success("角色删除成功,删除[" + idList.size() + "]条记录");
        }
        return Result.fail("角色删除失败");
    }

    @PreAuthorize("hasAuthority('sys:role')")
    @ResponseBody
    @GetMapping
    public Result findPage() {
        // 组装分页信息
        PageParam pageParam = RequestUtil.packagePageParamByNumAndSize("page", "rows");
        // 组装排序信息
        String orderBy = RequestUtil.packageOrderBy("sort", "order", true);
        // 得到所有的查询参数，注意也包括分页信息和排序信息
        QueryParam param = RequestUtil.getQueryParam();

        IPage<SysRole> page = sysRoleService.findPage(param, pageParam, orderBy);
        GridTable<SysRole> table = new GridTable<>(page.getTotal(), page.getRecords());

        return Result.success(table);
    }

    @PreAuthorize("hasAuthority('sys:role')")
    @ResponseBody
    @GetMapping("/findUserAndAcl")
    public Result findUserAndAcl(@RequestParam Long roleId) {
        List<SysUser> userList = sysUserService.findByRoleId(roleId);
        List<SysAcl> aclList = sysAclService.findByRoleId(roleId);
        Map<String, Object> data = new HashMap<>(16);
        data.put("userList", userList);
        data.put("aclTree", TreeUtil.bulidTree(aclList));
        return Result.success(data);
    }

}